package com.dhu.service.impl;

import com.dhu.dao.PostMapper;
import com.dhu.dao.UserLikePostMapper;
import com.dhu.pojo.UserLikePost;
import com.dhu.service.UserLikePostService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserLikePostServiceImpl implements UserLikePostService {
    @Autowired
    UserLikePostMapper userLikePostMapper;
    @Autowired
    PostMapper postMapper;

    @Override
    public UserLikePost isLike(int userId, int postId) {
        try {
            return userLikePostMapper.isLike(userId, postId);
        } catch (Exception e) {
            return null;
        }
    }

    @Override
    public int changeLike(boolean isLike, int userId, int postId) {
        try {
            if (isLike) {
                // 帖子点赞数-1
                postMapper.changeLikes(postId, -1);
                return userLikePostMapper.disLike(userId, postId);
            } else {
                // 帖子点赞数+1
                postMapper.changeLikes(postId, 1);
                return userLikePostMapper.like(userId, postId);
            }
        } catch (Exception e) {
            return -1;
        }
    }
}
